package com.es.servlet; /**
 * 描述:
 * MyBatis-
 *
 * @author Derek
 * 创建时间：2022-05-07 10:33
 */

import com.alibaba.fastjson.JSONObject;
import com.es.entity.Student;
import com.es.service.StudentService;
import com.es.service.impl.StudentServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@WebServlet(name = "StudentServlet", value = "/student")
public class StudentServlet extends BaseServlet {

    StudentService service = new StudentServiceImpl();

    @Override
    protected void get(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Integer id = Integer.valueOf(req.getParameter("id"));
        Student student = service.get(id);
        resp.setContentType("text/plain;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.print(JSONObject.toJSON(student).toString());
        out.flush();
    }

    @Override
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("我是保存的方法");
        String name = req.getParameter("name");
        String birthDate = req.getParameter("birthDate");
        int id = Integer.parseInt(req.getParameter("id"));
        //
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Student student = new Student();
        student.setName(name);
        student.setId(id);
        try {
            student.setBirthDate(simpleDateFormat.parse(birthDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int i = service.update(student);
        if(i>0){
            req.setAttribute("msg", "更新成功，您的学号是："+student.getId());
        }else{
            req.setAttribute("msg", "更新失败");

        }
        req.getRequestDispatcher("student?op=103").forward(req,resp);
    }

    @Override
    public void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("我是保存的方法");
        String name = req.getParameter("name");
        String birthDate = req.getParameter("birthDate");
        //
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Student student = new Student();
        student.setName(name);
        try {
            student.setBirthDate(simpleDateFormat.parse(birthDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int i = service.save(student);
        if(i>0){
            req.setAttribute("msg", "保存成功，您的学号是："+student.getId());
        }else{
            req.setAttribute("msg", "保存失败");

        }
        req.getRequestDispatcher("student?op=103").forward(req,resp);
    }

    @Override
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int id = Integer.parseInt(req.getParameter("id"));
        int i = service.delete(id);
        if(i>0){
            req.setAttribute("msg", "删除成功");
        }else{
            req.setAttribute("msg", "删除失败");

        }
        req.getRequestDispatcher("student?op=103").forward(req,resp);
    }

    @Override
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("我是list方法");
        List<Student> stus = service.list();
        req.setAttribute("stus", stus);
        req.getRequestDispatcher("student.jsp").forward(req,resp);
    }

    @Override
    protected void listByCondition(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("我是listByCondition方法");
    }
}
